package com.eflying.book.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eflying.book.bean.Book;
import com.eflying.book.service.BookService;
import com.eflying.pager.PageBean;

import cn.itcast.servlet.BaseServlet;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;


@WebServlet("/BookServlet")
public class BookServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private BookService bookService = new BookService();
	


	/**
	 * 获取当前页码
	 * @param req
	 * @param resp
	 * @return
	 */
	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if(param != null && !param.trim().isEmpty()){
			try {
				pc = Integer.parseInt(param);
			} catch (RuntimeException e) {
			}
		}
		return pc;
	}
	/**
	 * 获取URL
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	/*
	 * http://localhost:18180/goods/BookServlet?method=findByCategory&cid=xxx&pc=3;
	 * getRequestURI()=/goods/BookServlet
	 * getQueryString()=method=findByCategory&cid=xxx&pc=3
	 */
	private String getUrl(HttpServletRequest req) {
		String url = req.getRequestURI() + "?" + req.getQueryString();
		/*
		 * 如果存在pc参数，则拿掉
		 */
		int index = url.lastIndexOf("&pc=");
		if (index != -1) {
			url = url.substring(0, index);
		}
		return url;
	}
	/**
	 * 按分类查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1.得到PC，使用页面传递的PC，如果没有则为1
		 */
		int pc = getPc(request);
		/*
		 * 2.得到url
		 */
		String url = getUrl(request);
		/*
		 * 3.得到查询条件（本方法按分类查）
		 */
		String cid = request.getParameter("cid");
		/*
		 * 4.使用PC，调用service的方法findByCategory();
		 */
		PageBean<Book> pb = bookService.findByCategory(cid, pc);
		/*
		 * 5.给PageBean设置URL，保存PageBean，转发到jsps/book/list.jsp
		 */
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return"f:/jsps/book/list.jsp";
	}
	/**
	 * 按作者查
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByAuthor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1.得到PC，使用页面传递的PC，如果没有则为1
		 */
		int pc = getPc(request);
		/*
		 * 2.得到url
		 */
		String url = getUrl(request);
		/*
		 * 3.得到查询条件（本方法按分类查）
		 */
		String author = request.getParameter("author");
		/*
		 * 4.使用PC，调用service的方法findByCategory();
		 */
		PageBean<Book> pb = bookService.findByAuthor(author, pc);
		/*
		 * 5.给PageBean设置URL，保存PageBean，转发到jsps/book/list.jsp
		 */
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return"f:/jsps/book/list.jsp";
	}
	/**
	 * 按出版社
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByPress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1.得到PC，使用页面传递的PC，如果没有则为1
		 */
		int pc = getPc(request);
		/*
		 * 2.得到url
		 */
		String url = getUrl(request);
		/*
		 * 3.得到查询条件（本方法按分类查）
		 */
		String press = request.getParameter("press");
		/*
		 * 4.使用PC，调用service的方法findByCategory();
		 */
		PageBean<Book> pb = bookService.findByPress(press, pc);
		/*
		 * 5.给PageBean设置URL，保存PageBean，转发到jsps/book/list.jsp
		 */
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return"f:/jsps/book/list.jsp";
	}
	/**
	 * 高级查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCombination(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1.得到PC，使用页面传递的PC，如果没有则为1
		 */
		int pc = getPc(request);
		/*
		 * 2.得到url
		 */
		String url = getUrl(request);
		/*
		 * 3.得到查询条件（本方法按分类查）
		 */
		Book book = new Book();
		String press = request.getParameter("press");
		String author = request.getParameter("author");
		String bname = request.getParameter("bname");
		if(press!=null)
			book.setPress(press);
		if(author!=null)
			book.setAuthor(author);
		if(bname!=null)
			book.setBname(bname);
		/*
		 * 4.使用PC，调用service的方法findByCategory();
		 */
		PageBean<Book> pb = bookService.findByCombination(book, pc);
		/*
		 * 5.给PageBean设置URL，保存PageBean，转发到jsps/book/list.jsp
		 */
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return"f:/jsps/book/list.jsp";
	}
	/**
	 * 按书名
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 1.得到PC，使用页面传递的PC，如果没有则为1
		 */
		int pc = getPc(request);
		/*
		 * 2.得到url
		 */
		String url = getUrl(request);
		/*
		 * 3.得到查询条件（本方法按分类查）
		 */
		String bname = request.getParameter("bname");
		/*
		 * 4.使用PC，调用service的方法findByCategory();
		 */
		PageBean<Book> pb = bookService.findByBname(bname, pc);
		/*
		 * 5.给PageBean设置URL，保存PageBean，转发到jsps/book/list.jsp
		 */
		pb.setUrl(url);
		request.setAttribute("pb", pb);
		return"f:/jsps/book/list.jsp";
	}

}
